import { defineComponent } from 'vue';
import { homeLogin,auth_login } from "@/api/home"
// @ts-ignore
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
import { ElConfigProvider } from 'element-plus';
import loading from "@/utils/loading";
import { dingdingLinkOpen } from '@/utils';
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
const isQiankun = qiankunWindow.__POWERED_BY_QIANKUN__;

export default defineComponent({
    name: 'Home',
    setup() {

        const store = useStore();
        const route = useRoute();
        const router = useRouter();
        const dingdingInfo = computed(() => store.state.dingdingInfo) as AnyObject;
        const isReady = ref(false);

        const checkCode = async () => {
             console.log('====tcpi index===',isQiankun,qiankunWindow.__POWERED_BY_QIANKUN__);
            if(isQiankun){
                loading.show();
                const { data, code } = await auth_login({token:window.localStorage.tc_all_token});
                store.commit("storeDingdingInfo", { ...data });
                router.push(route.path);
                isReady.value = true;
                loading.hide();
            }else{
                if (route.query.authCode) {
                    loading.show();
                    const { data, code } = await homeLogin(route.query.authCode as string);
                    store.commit("storeDingdingInfo", { authCode: route.query.authCode, ...data });
                    router.push(route.path)
                    isReady.value = true;
                    loading.hide();
                } else {
                    if (!dingdingInfo.value.authCode) {
                        dingdingLinkOpen();
                    } else {
                        isReady.value = true;
                    }
                }
            }
            

        }
        checkCode();

        return () => (

            <div><ElConfigProvider locale={zhCn}>
                {isReady.value && <router-view></router-view>}
            </ElConfigProvider>
            </div>
        );
    }
});
